package org.edu.modules.sxjy.controller;

import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.edu.common.api.vo.Result;
import org.edu.common.aspect.annotation.AutoLog;
import org.edu.common.system.base.controller.BaseController;
import org.edu.common.system.vo.LoginUser;
import org.edu.modules.sxjy.common.noresubmit.CacheLock;
import org.edu.modules.sxjy.entity.VeApply;
import org.edu.modules.sxjy.entity.VeCommon;
import org.edu.modules.sxjy.entity.VeCompany;
import org.edu.modules.sxjy.entity.VePost;
import org.edu.modules.sxjy.entity.VePractice;
import org.edu.modules.sxjy.entity.VeStudent;
import org.edu.modules.sxjy.service.CacheKeyGenerator;
import org.edu.modules.sxjy.service.IVeApplyService;
import org.edu.modules.sxjy.service.IVePracticeService;
import org.edu.modules.sxjy.service.impl.CacheKeyGeneratorImp;
import org.edu.modules.sxjy.utils.PageResponseResult;
import org.edu.modules.sxjy.vo.VePracticeAddVo;
import org.edu.modules.sxjy.vo.VePracticeInfoVo;
import org.edu.modules.sxjy.vo.VePracticeReqVo;
import org.edu.modules.sxjy.vo.VePracticeUpdateVo;
import org.edu.modules.sxjy.vo.VePracticeVo;
import org.edu.modules.sxjy.vo.VePracticeVoXls;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

@Api(tags={"学生实习安排查询"})
@RestController
@RequestMapping({"/sxjy/vePractice"})
@Component
@ApiSort(60)
public class VePracticeController
        extends BaseController<VePractice, IVePracticeService>
{
    private static final Logger log = LoggerFactory.getLogger(VePracticeController.class);
    @Autowired
    private IVePracticeService vePracticeService;
    @Autowired
    private IVeApplyService veApplyService;

    @Bean
    public CacheKeyGenerator cacheKeyGenerator()
    {
        return new CacheKeyGeneratorImp();
    }

    @AutoLog("学生实习安排查询-导入数据")
    @ApiOperation(value="导入数据", notes="学生实习安排查询-导入数据")
    @RequestMapping(value={"/importExcel"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
    @CacheLock(prefix="redisLock.test", expire=1)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response)
    {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest)request;
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        Iterator localIterator = fileMap.entrySet().iterator();
        if (localIterator.hasNext())
        {
            Map.Entry<String, MultipartFile> entity = (Map.Entry)localIterator.next();
            MultipartFile file = (MultipartFile)entity.getValue();
            ImportParams params = new ImportParams();
            params.setTitleRows(1);
            params.setHeadRows(1);
            params.setNeedSave(true);
            int size = 0;
            try
            {
                List<VePracticeVoXls> listSysCategorys = ExcelImportUtil.importExcel(file.getInputStream(), VePracticeVoXls.class, params);
                //lbh 2024/07/31 localObject1 >> Iterator localObject1
                for (Iterator localObject1 = listSysCategorys.iterator(); ((Iterator)localObject1).hasNext();)
                {
                    VePracticeVoXls vePracticeVoXls = (VePracticeVoXls)((Iterator)localObject1).next();
                    boolean b = this.vePracticeService.setExcel(vePracticeVoXls);
                    if (b) {
                        size++;
                    }
                }
                return Result.ok("文件导入成功！数据行数：" + size);
            }
            catch (Exception e)
            {
                Object localObject1;
                log.error(e.getMessage(), e);
                return Result.error("文件导入失败：" + e.getMessage());
            }
            finally
            {
                try
                {
                    file.getInputStream().close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
        return Result.error("文件导入失败！");
    }

    @AutoLog("学生实习安排查询-导出数据")
    @ApiOperation(value="导出数据", notes="学生实习安排查询-导出数据")
    @PostMapping({"/exportXls"})
    @CacheLock(prefix="redisLock.test", expire=1)
    public ModelAndView exportXls(@RequestBody VeCommon veCommon)
    {
        List<VePracticeVoXls> vePracticeVoXls = this.vePracticeService.exportXls(veCommon);

        ModelAndView modelAndView = new ModelAndView(new JeecgEntityExcelView());

        modelAndView.addObject("fileName", "学生实习安排信息");
        modelAndView.addObject("entity", VePracticeVoXls.class);
        LoginUser user = (LoginUser)SecurityUtils.getSubject().getPrincipal();
        modelAndView.addObject("params", new ExportParams("学生实习安排列表", "导出人:" + user.getRealname(), "导出信息"));
        modelAndView.addObject("data", vePracticeVoXls);
        return modelAndView;
    }

    @AutoLog("学生实习安排查询-查询列表")
    @ApiOperation(value="查询列表", notes="学生实习安排查询-查询列表")
    @GetMapping({"/selectAll"})
    public PageResponseResult selectAll(VePracticeVo vePracticeVo, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo, @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, VeCommon veCommon)
    {
        Integer startLine = Integer.valueOf((pageNo.intValue() - 1) * pageSize.intValue());
        List<VePracticeVo> vePracticeVoList = this.vePracticeService.selectAll(vePracticeVo, startLine, pageSize, veCommon);
        int total = this.vePracticeService.getSumPage(vePracticeVo, veCommon);
        if ((Objects.isNull(vePracticeVoList)) && (vePracticeVoList.size() <= 0)) {
            return new PageResponseResult(Integer.valueOf(1), 1, 0, Result.OK());
        }
        double totals = total;
        double pageSizes = pageSize.intValue();
        double totalPage = Math.ceil(totals / pageSizes);
        return new PageResponseResult(pageNo, (int)totalPage, total, Result.OK(vePracticeVoList));
    }

    @AutoLog("学生实习安排查询-获得学生id及名称")
    @ApiOperation(value="获得学生id及名称", notes="学生实习安排查询-获得学生id及名称")
    @GetMapping({"/getStuName"})
    public List<VeStudent> getStuName(String name)
    {
        List<VeStudent> stuNameList = this.vePracticeService.getStuName(name);

        return stuNameList;
    }

    @AutoLog("学生实习安排查询-获得企业名称以及id")
    @ApiOperation(value="获得企业名称以及id", notes="学生实习安排查询-获得企业名称以及id")
    @GetMapping({"/getCpName"})
    public List<VeCompany> getCpName(String name)
    {
        List<VeCompany> cpNameList = this.vePracticeService.getCpName(name);

        return cpNameList;
    }

    @AutoLog("学生实习安排查询-获得岗位名称以及id")
    @ApiOperation(value="获得岗位名称以及id", notes="学生实习安排查询-获得岗位名称以及id")
    @GetMapping({"/getPostName"})
    public List<VePost> getPostName(String name)
    {
        List<VePost> postNameList = this.vePracticeService.getPostName(name);

        return postNameList;
    }

    @AutoLog("学生实习安排查询-新增信息")
    @ApiOperation(value="新增信息", notes="学生实习安排查询-新增信息")
    @PostMapping({"/add"})
    @CacheLock(prefix="redisLock.test", expire=1)
    public Result<?> add(@RequestBody VePracticeAddVo vePracticeAddVo)
    {
        List<VePractice> vePracticeList = this.vePracticeService.list();
        for (VePractice vePractice : vePracticeList) {
            if (vePractice.getStuUserId() == vePracticeAddVo.getStuUserId()) {
                this.vePracticeService.removeById(Integer.valueOf(vePractice.getId()));
            }
        }
        if (vePracticeAddVo != null)
        {
            this.vePracticeService.add(vePracticeAddVo);
            return Result.OK("添加成功！");
        }
        return Result.error("添加失败");
    }

    @AutoLog("学生实习安排查询-修改信息")
    @ApiOperation(value="修改信息", notes="学生实习安排查询-修改信息")
    @PutMapping({"/udpate"})
    @CacheLock(prefix="redisLock.test", expire=1)
    public Result<?> update(@RequestBody VePracticeUpdateVo vePracticeUpdateVo)
    {
        if (vePracticeUpdateVo != null)
        {
            this.vePracticeService.updates(vePracticeUpdateVo);
            return Result.OK("编辑成功！");
        }
        return Result.error("编辑失败");
    }

    @AutoLog("学生实习安排查询-编辑回显")
    @ApiOperation(value="编辑回显", notes="学生实习安排查询-编辑回显")
    @GetMapping({"/udpateEcho"})
    public Result<?> updateEcho(int id)
    {
        if (id != 0)
        {
            VePracticeUpdateVo vePracticeUpdateVo = this.vePracticeService.updateEcho(id);
            return Result.OK(vePracticeUpdateVo);
        }
        return Result.error("编辑失败");
    }

    @AutoLog("学生实习安排查询-学生提交表编辑回显")
    @ApiOperation(value="学生提交表编辑回显", notes="学生实习安排查询-学生提交表编辑回显")
    @GetMapping({"/stuSubmitTableEcho"})
    public Result<?> stuSubmitTableEcho(String userId)
    {
        return this.vePracticeService.stuSubmitTableEcho(userId);
    }

    @AutoLog("学生实习安排查询-查看详细信息")
    @ApiOperation(value="查看详细信息", notes="学生实习安排查询-查看详细信息")
    @GetMapping({"/selectOneInfo"})
    public Result<?> selectOneInfo(int id)
    {
        if (id != 0)
        {
            VePracticeInfoVo vePracticeInfoVo = this.vePracticeService.getOneInfo(id);
            if (vePracticeInfoVo == null) {
                return Result.OK("学生信息有误");
            }
            return Result.OK(vePracticeInfoVo);
        }
        return Result.OK("查看失败");
    }

    @AutoLog("学生实习安排查询-查看提交学生信息")
    @ApiOperation(value="查看提交学生信息", notes="学生实习安排查询-查看提交学生信息")
    @GetMapping({"/stuOneInfo"})
    public Result<?> stuOneInfo(String userId)
    {
        if (userId != null)
        {
            VePracticeInfoVo stuOneInfo = this.vePracticeService.getStuOneInfo(userId);
            return Result.OK(stuOneInfo);
        }
        return Result.OK("查看失败");
    }

    @AutoLog("学生实习安排查询-提交登记表")
    @ApiOperation(value="提交登记表", notes="学生实习安排查询-提交登记表")
    @PutMapping({"/addRegister"})
    public Result<?> addRegister(@RequestBody VePracticeReqVo vePracticeReqVo)
    {
        VeApply veApply = this.veApplyService.getApplyByUserId(vePracticeReqVo.getUserId(), Integer.valueOf(0));
        if (veApply != null) {
            return Result.error("实习申请审核中! ");
        }
        VeApply veApply1 = this.veApplyService.getApplyByUserId(vePracticeReqVo.getUserId(), Integer.valueOf(1));
        if (veApply1 != null) {
            return Result.error("实习申请已通过! ");
        }
        if ((vePracticeReqVo != null) && (!"".equals(vePracticeReqVo)))
        {
            this.vePracticeService.addRegister(vePracticeReqVo);
            return Result.OK("提交成功！");
        }
        return Result.error("提交失败");
    }

    @AutoLog("学生实习安排查询-通过id删除")
    @ApiOperation(value="通过id删除", notes="学生实习安排查询-通过id删除")
    @DeleteMapping({"/delete"})
    public Result<?> delete(@RequestParam(name="id", required=true) String id)
    {
        return this.vePracticeService.delete(id);
    }

    @AutoLog("学生实习安排查询-批量删除")
    @ApiOperation(value="批量删除", notes="学生实习安排查询-批量删除")
    @DeleteMapping({"/deleteBatch"})
    public Result<?> deleteBatch(@RequestParam(name="ids", required=true) String ids)
    {
        return this.vePracticeService.deleeteBatch(ids);
    }

    @AutoLog("学生实习安排查询-根据学生id获取学生申请信息")
    @ApiOperation(value="根据学生id获取学生申请信息", notes="学生实习安排查询-根据学生id获取学生申请信息")
    @GetMapping({"/getStuApplyInfo"})
    public Result<?> getStuApplyInfo(@RequestParam(name="stuId") int stuId)
    {
        return this.vePracticeService.getStuApplyInfo(stuId);
    }

    @AutoLog("学生实习安排查询-根据学生id判断该学生是否已经安排")
    @ApiOperation(value="根据学生id判断该学生是否已经安排", notes="学生实习安排查询-根据学生id判断该学生是否已经安排")
    @GetMapping({"/stuIsPracticeByStuId"})
    public Result<?> stuIsPracticeByStuId(@RequestParam(name="stuId") int stuId)
    {
        return this.vePracticeService.stuIsPracticeByStuId(stuId);
    }

    @AutoLog("学生实习安排查询-获取没登记过的学生信息列表")
    @ApiOperation(value="获取没登记过的学生信息列表", notes="学生实习安排查询-获取没登记过的学生信息列表")
    @GetMapping({"/getNoPracticeStuList"})
    public PageResponseResult getNoPracticeStuList(@RequestParam(name="stuName", required=false) String stuName, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo, @RequestParam(name="pageSize", defaultValue="10") Integer pageSize)
    {
        return this.vePracticeService.getNoPracticeStuList(stuName, pageNo, pageSize);
    }

    @AutoLog("学生实习安排查询-根据学生表id查询实习信息")
    @ApiOperation(value="根据学生表id查询实习信息", notes="学生实习安排查询-根据学生表id查询实习信息")
    @GetMapping({"/getPracticeByStuUserId"})
    public Result<?> getPracticeByStuUserId(int stuUserId)
    {
        return this.vePracticeService.getPracticeByStuUserId(stuUserId);
    }
}
